package cn.com.cym.gamezao.web.demo;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.demo.Child;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.service.demo.ChildService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "x-子类控制类")
@RestController
@RequestMapping(value = "child")
public class ChildController {

    final ChildService thisService;

    public ChildController(ChildService childService){
        this.thisService=childService;
    }


    @ApiImplicitParam(name = "code", value = "code", required = true,dataType = "String")
    @ApiOperation(value = "按照编码获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/code/{code}")
    public ResponseEntity<?> getByCode(@PathVariable String code){
        Child child = this.thisService.getByCode(code);
        return ResponseEntity.ok(child);
    }

    @ApiImplicitParam(name = "parentId", value = "parentId", required = true,dataType = "String")
    @ApiOperation(value = "按照字典编码获子类数据集")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @GetMapping("/parent/{parentId}")
    public ResponseEntity<?> getByDictCode(@PathVariable Long parentId){
        List<Child> children = this.thisService.getByParentId(parentId);
        return ResponseEntity.ok(children);
    }


    @ApiImplicitParam(name = "child", value = "child", required = true,dataType = "Child")
    @ApiOperation(value = "新建子类数据")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PostMapping()
    @AuthToken(rule = "demo.child.add")
    public ResponseEntity<?> add(@RequestBody Child child){
        if(child.getParent()==null){
            throw new BaseException("不存在主表数据，无法新建",new ResultResponse("不存在主表数据，无法新建", HttpStatus.BAD_REQUEST.value()));
        }
        final Child storeChild = this.thisService.save(child);
        return ResponseEntity.ok(storeChild);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code",paramType = "path", required = true,dataType = "String"),
            @ApiImplicitParam(name = "child", value = "child",paramType = "", required = true,dataType = "Child")})
    @ApiOperation(value = "修改子类数据")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PutMapping("/{code}")
    @AuthToken(rule = "demo.child.update")
    public ResponseEntity<?> update(@RequestBody Child child,@PathVariable String code){
        if(child.getParent()==null){
            throw new BaseException("不存在主表数据，无法更新",new ResultResponse("不存在主表数据，无法更新", HttpStatus.BAD_REQUEST.value()));
        }
        final Child storeChild = this.thisService.updateByCode(child,code);
        return ResponseEntity.ok(storeChild);
    }


    @ApiImplicitParam(name = "id", value = "id",paramType = "path",required = true,dataType = "Long")
    @ApiOperation(value = "删除子类")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @DeleteMapping("/{id}")
    @AuthToken(rule = "demo.child.delete")
    public ResponseEntity<?> delete(@PathVariable Long id){
        this.thisService.physicalDelete(id);
        return ResponseEntity.ok(new ResultResponse("删除成功", HttpStatus.OK.value()));
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<?> byPageable = this.thisService.findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }
}
